package com.right.health.app.ui.me.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.magic.basic.adapter.BaseAbsAdapter;
import com.right.health.app.R;
import com.right.health.app.bean.AddressListResponse;

/**
 * @author weming
 * @Date 2021/3/29
 * @description 收货地址适配器
 */
public class ShippingAddressAdapter extends BaseAbsAdapter<AddressListResponse.UserAddressBean> {

    private int mDefaultIndex=-1;
    private View.OnClickListener mClickListener;

    public ShippingAddressAdapter(Context context) {
        super(context);
    }


    public void setOnClickListener(View.OnClickListener listener) {
        this.mClickListener = listener;
    }

    public int getDefaultIndex() {
        return mDefaultIndex;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_shipping_address, null);
            viewHolder = new ViewHolder();
            viewHolder.mNameTv = convertView.findViewById(R.id.tv_name);
            viewHolder.mPhoneTv = convertView.findViewById(R.id.tv_phone);
            viewHolder.mAddressTv = convertView.findViewById(R.id.tv_address);
            viewHolder.mEditTv = convertView.findViewById(R.id.tv_edit);
            viewHolder.mDeleteTv = convertView.findViewById(R.id.tv_delete);
            viewHolder.mDefaultTv = convertView.findViewById(R.id.tv_default);
            viewHolder.mCheckIv = convertView.findViewById(R.id.iv_check);
            if (mClickListener != null) {
                viewHolder.mDefaultTv.setOnClickListener(mClickListener);
                viewHolder.mEditTv.setOnClickListener(mClickListener);
                viewHolder.mDeleteTv.setOnClickListener(mClickListener);
                viewHolder.mCheckIv.setOnClickListener(mClickListener);
            }
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mDefaultTv.setTag(position);
        viewHolder.mEditTv.setTag(position);
        viewHolder.mDeleteTv.setTag(position);
        viewHolder.mCheckIv.setTag(position);
        AddressListResponse.UserAddressBean bean=mDataSource.get(position);
        viewHolder.mNameTv.setText(bean.getUsername());
        viewHolder.mPhoneTv.setText(bean.getUserphone());
        viewHolder.mAddressTv.setText(bean.getUseraddress());
        if ("1".equals(bean.getIsdefault())) {
            viewHolder.mCheckIv.setImageResource(R.mipmap.ic_check_on);
            viewHolder.mDefaultTv.setText("默认地址");
            viewHolder.mDefaultTv.setTextColor(Color.RED);
            mDefaultIndex=position;
        }else {
            viewHolder.mCheckIv.setImageResource(R.mipmap.ic_check_off);
            viewHolder.mDefaultTv.setText("设为默认地址");
            viewHolder.mDefaultTv.setTextColor(Color.parseColor("#777777"));
        }
        return convertView;
    }

    static class ViewHolder {
        private TextView mNameTv, mPhoneTv, mAddressTv, mEditTv, mDeleteTv, mDefaultTv;
        private ImageView mCheckIv;
    }
}
